/*
 * Copyright 2011 derek.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aocore.item;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author derek
 */
public abstract class BaseItem
{
    private static final List<BaseItem> baseItems;

    static
    {
        baseItems = new ArrayList<BaseItem>();

        try
        {
            FileReader fr = new FileReader("./data/misc.txt");
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();

            while ((line = br.readLine()) != null)
            {
                try
                {
                    baseItems.add(new BaseMiscItem(line));
                }
                catch (Exception e)
                {
                    e.printStackTrace(System.err);
                }
            }

            fr = new FileReader("./data/armor.txt");
            br = new BufferedReader(fr);
            line = br.readLine();

            while ((line = br.readLine()) != null)
            {
                try
                {
                    baseItems.add(new BaseArmor(line));
                }
                catch (Exception e)
                {
                    e.printStackTrace(System.err);
                }
            }

            fr = new FileReader("./data/weapons.txt");
            br = new BufferedReader(fr);
            line = br.readLine();

            while ((line = br.readLine()) != null)
            {
                try
                {
                    baseItems.add(new BaseWeapon(line));
                }
                catch (Exception e)
                {
                    e.printStackTrace(System.err);
                }
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace(System.err);
        }
        catch (IOException e)
        {
            e.printStackTrace(System.err);
        }
    }

    protected String code;
    //protected ItemType type;
    protected boolean stackable;
    protected boolean useable;

    public static BaseItem get(String code)
    {
        for (BaseItem baseItem : baseItems)
        {
            if (baseItem.code.equals(code))
            {
                return baseItem;
            }
        }

        return null;
    }

    @Override
    public String toString()
    {
        return code;
    }

    public String getCode()
    {
        return code;
    }

    public boolean isStackable()
    {
        return stackable;
    }

    public boolean isUseable()
    {
        return useable;
    }
}
